home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / presto / prest_04.lha / src / threadq.h < prev    next >
C/C++ Source or Header  |  1989-06-06  |  3KB  |  197 lines

  1.  
  2. //
  3. // Thread Q stuff
  4. //
  5.  
  6.  
  7. //
  8. // ThreadQUnlocked should be used when the threadq is already 
  9. // associated with a locked data structuire (such as a synchronization
  10. // object).  We save having to test for locking.
  11. //
  12.  
  13. class ThreadQUnlocked    : public Oqueue    {
  14.     int    tq_neededstate;
  15. public:
  16.     ThreadQUnlocked(int neededstate, Thread *t = 0);
  17.     ~ThreadQUnlocked();
  18.     inline Thread* get();
  19.     inline Thread*     lookat();
  20.     inline void        append(Thread* t);
  21.     inline void    prepend(Thread* t);
  22.     inline void     remove(Thread* t);
  23.     virtual void print(ostream& = cout);
  24. };
  25.  
  26. class ThreadQ    : public Oqueue    {
  27.     int        tq_neededstate;        // sanity check
  28.     int        tq_length;
  29.     Spinlock     *tq_lock;
  30.     inline        void lock();
  31.     inline        void unlock();
  32. public:
  33.     ThreadQ(int neededstate, Thread *t = 0);
  34.     ~ThreadQ();
  35.     inline Thread*    get();
  36.     inline Thread*     lookat();
  37.     inline void        append(Thread* t);
  38.     inline void    prepend(Thread* t);
  39.     inline void     remove(Thread* t);
  40.     int        length()
  41.                 { return tq_length; }
  42.     virtual void print(ostream& = cout);
  43. };
  44.  
  45.  
  46.  
  47. inline
  48. Thread*
  49. ThreadQUnlocked::get()
  50. {  
  51.     Thread* t = (Thread*)(Oqueue::get());
  52.     if (t==0 || t->t_state&tq_neededstate) 
  53.         return t;
  54.     else
  55.            t->error("get bad state");
  56. }
  57.  
  58. inline
  59. Thread*
  60. ThreadQUnlocked::lookat()
  61. {
  62.     Thread* t = (Thread*)(Oqueue::lookat());
  63.     if (t==0 || t->t_state&tq_neededstate)
  64.            return t;
  65.     else
  66.            t->error("get bad state");
  67. }
  68.  
  69.  
  70. inline
  71. void
  72. ThreadQUnlocked::append(Thread* t)
  73. {
  74.     if (t==0 || t->t_state&tq_neededstate)    {
  75.         Oqueue::append(t); 
  76.     } else
  77.         t->error("Bad state append");
  78. }
  79.  
  80.  
  81.  
  82. inline
  83. void
  84. ThreadQUnlocked::prepend(Thread* t)
  85. {
  86.     if (t==0 || t->t_state&tq_neededstate)    {
  87.         Oqueue::prepend(t); 
  88.     }  else
  89.           t->error("Bad state prepend");
  90. }
  91.  
  92.  
  93. inline
  94. void
  95. ThreadQUnlocked::remove(Thread *t)
  96.     if (t==0 || t->t_state&tq_neededstate)    {
  97.         Oqueue::remove(t); 
  98.     } else
  99.           t->error("Bad state remoeve");
  100. }
  101.  
  102.  
  103. //
  104. // Locked threadq's
  105. //
  106. inline
  107. void ThreadQ::lock()
  108. {
  109.     register Spinlock *sp = tq_lock; 
  110.     sp->lock(); 
  111. }
  112.  
  113. inline
  114. void ThreadQ::unlock()
  115. {
  116.     register Spinlock *sp = tq_lock; 
  117.     sp->unlock(); 
  118. }
  119.  
  120.  
  121.  
  122. inline
  123. Thread*
  124. ThreadQ::get()
  125. {  
  126.     lock();
  127.     Thread* t = (Thread*)(Oqueue::get());
  128.     if (t)
  129.         tq_length--;
  130.     unlock();
  131.     
  132.     if (t==0 || t->t_state&tq_neededstate) 
  133.         return t;
  134.     else
  135.            t->error("get bad state");
  136. }
  137.             
  138. inline
  139. Thread*
  140. ThreadQ::lookat()
  141. {
  142.     Thread* t = (Thread*)(Oqueue::lookat());
  143.     if (t==0 || t->t_state&tq_neededstate)
  144.            return t;
  145.     else
  146.            t->error("get bad state");
  147. }
  148.     
  149.  
  150. inline
  151. void
  152. ThreadQ::append(Thread* t)
  153. {
  154.     if (t && t->t_state&tq_neededstate)    {
  155.         lock();
  156.         Oqueue::append(t); 
  157.         tq_length++;
  158.         unlock();
  159.     } else
  160.         t->error("Bad state append");
  161. }
  162.  
  163.  
  164.  
  165. inline
  166. void
  167. ThreadQ::prepend(Thread* t)
  168. {
  169.     if (t && t->t_state&tq_neededstate)    {
  170.         lock();
  171.         Oqueue::prepend(t);
  172.         tq_length++; 
  173.         unlock();
  174.     }  else
  175.           t->error("Bad state prepend");
  176. }
  177.  
  178. inline
  179. void
  180. ThreadQ::remove(Thread *t)
  181.     if (t && t->t_state&tq_neededstate)    {
  182.         lock();
  183.         Oqueue::remove(t); 
  184.         tq_length--;
  185.         unlock();
  186.     } else
  187.           t->error("Bad state remoeve");
  188. }
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.